package com.linyaonan.leetcode.medium._151;

import java.util.Arrays;
import java.util.StringJoiner;

/**
 * @author: Lin
 * @date: 2020/4/10
 */
public class ReverseWords {
    public String reverseWords(String s) {
        if (s == null) {
            return null;
        }
        s = s.trim();
        if ("".equals(s)) {
            return "";
        }
        String[] split = s.split("\\s+", -1);
        StringBuilder sb = new StringBuilder();
        for (int i = split.length - 1; i >= 0; i--) {
            sb.append(split[i]).append(" ");
        }
        sb.delete(sb.length() - 1, sb.length());
        return sb.toString();
    }

    public String reverseWords2(String s) {
        if (s == null) {
            return null;
        }
        s = s.trim();
        if ("".equals(s)) {
            return "";
        }
        String[] split = s.split("\\s+", -1);
        StringJoiner joiner = new StringJoiner(" ");

        for (int i = split.length - 1; i >= 0; i--) {
            joiner.add(split[i]);
        }
        return joiner.toString();
    }

}
